/**
 * @filename:ShebeiServiceImpl Dec 21, 2023
 * @project Wonder Vue Admin Api  1.1.0
 * Copyright(c) 2022 Wonder Co. Ltd. 
 * All right reserved. 
 */
package com.wonder.app.service.impl;

import com.wonder.app.domain.Shebei;
import com.wonder.app.mapper.ShebeiDao;
import com.wonder.app.service.ShebeiService;
import com.wonder.app.request.ShebeiRequest;
import com.wonder.common.utils.StringUtils;
import com.wonder.common.utils.MessageUtils;
import com.wonder.common.common.AjaxResult;

import java.util.Date;
import java.util.Map;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

/**   
 * @Description:TODO(设备信息服务实现)
 *
 * @version: 1.1.0
 * @author: Wonder
 * 
 */
@Service
public class ShebeiServiceImpl  extends ServiceImpl<ShebeiDao, Shebei> implements ShebeiService  {

	@Autowired
	private ShebeiService shebeiService;

	@Autowired
	private ShebeiDao shebeiDao;
	
	
	
    /**
     * 验证设备信息信息
     * 
     * @param shebei
     * @param type
     * @return String
     */
    @Override
    public String validate(Shebei shebei,String type){
    
       if(type.equals("new")){     
          if(this.count(new QueryWrapper<Shebei>().eq("billno", shebei.getBillno())) >0)
    	   return "编号"+MessageUtils.message("not.unique"); 
    	   }  
       if(type.equals("update")){     
          if(this.count(new QueryWrapper<Shebei>().eq("billno", shebei.getBillno())
          .ne("fid",shebei.getFid())) >0)
    	   return "编号"+MessageUtils.message("not.unique"); 
    	   } 	    
    
       return "";
    }
    
    
    /**
	 * Description:设备信息分页返回
	 * @param request //检索条件
	 * @param page //分页参数
	 * @return IPage<Shebei>   //分页记录
	 */
	@Override 
	public IPage<Shebei> selectList(ShebeiRequest request, Page<Shebei> page){
	    LambdaQueryWrapper<Shebei> lambdaQueryWrapper = new LambdaQueryWrapper<>();
	    
	    if (null != request.getFid()) {
	       lambdaQueryWrapper.eq(Shebei::getFid, request.getFid());
	      }               
             
        
		lambdaQueryWrapper.orderByDesc(Shebei::getFid);	
		Page<Shebei> selectPage = this.baseMapper.selectPage(page, lambdaQueryWrapper);
		return selectPage;
    }


	/**
	 * Description:提交
	 */
	@Override
	public boolean shebeiTiJiao(Long fid) {
		Shebei shebei = shebeiDao.selectById(fid);

		//获取提交和审核状态
		boolean tj = shebei.getTijiao();
		boolean res ;
		//判断提交状态是 true 是停止提交
		if(tj == true ){
			res = false;
		}else{
			shebei.setTijiao(true);//提交状态
			res = shebeiService.updateById(shebei);
		}
		return res;
	}

	/**
	 * Description:撤销 成功后更新提交状态状态
	 */
	@Override
	public boolean shebeiCheXiao(Long fid) {
		Shebei shebei = shebeiDao.selectById(fid);

		//获取提交和审核状态
		boolean tj = shebei.getTijiao();
		boolean sh = shebei.getShenhe();
		boolean res ;
		if(tj == true && sh == false){
			shebei.setTijiao(false);
			res = shebeiService.updateById(shebei);
			//true;
		}else{
			res = false ;
		}

		return res;
	}


	/**
	 * Description:审核
	 */
	@Override
	public boolean shebeiShenHe(Long fid) {
		Shebei shebei = shebeiDao.selectById(fid);

		boolean tj = shebei.getTijiao();
		boolean sh = shebei.getShenhe();
		boolean res ;
		//获取当前日期，并给审核日期赋值
		Date currentDate = new Date();
		shebei.setAuditdate(currentDate);

		//当提交和审核状态是 true的时候，停止审核操作
		if(tj == true && sh == true  ){
			res = false;
		}
		else if(tj == true && sh == false ){
			shebei.setShenhe(true);
			res = shebeiService.updateById(shebei);
		}
		else{
			res = false;
		}
		return res;
	}


	/**
	 * Description:反审核 成功后更新提交和审核状态
	 */
	@Override
	public boolean shebeiFanShenHe(Long fid) {
		Shebei shebei = shebeiDao.selectById(fid);
		boolean tj = shebei.getTijiao();
		boolean sh = shebei.getShenhe();
		boolean res ;

		if(tj == true && sh == true ){
			//撤销成功时将提交和撤销状态同时更新
			shebei.setTijiao(false);//提交状态
			shebei.setShenhe(false);//撤销状态
			shebeiService.updateById(shebei);
			res = true;
		}else {
			res = false;
		}
		return res;
	}
}